//获取下拉菜单按钮
var more_down = document.querySelector("#more_down");
//获取下拉菜单
var more = document.querySelector(".more");
// more_down鼠标经过显示more
more_down.addEventListener("mouseover", function () {
  more.style.display = "block";
});
// more_down鼠标离开隐藏more
more_down.addEventListener("mouseout", function () {
  more.style.display = "none";
});
//鼠标移入more 显示more
more.addEventListener("mouseover", function () {
  more.style.display = "block";
});
//鼠标移出more 隐藏more
more.addEventListener("mouseout", function () {
  more.style.display = "none";
});

//dom
var login_btn = document.querySelector(".login_btn");
var logout_btn = document.querySelector(".logout_btn");
var user_username = document.querySelector(".user_username");
var user_info = document.querySelector(".user_info");
var user_show_img = document.querySelector(".user_show_img");
logout_btn.addEventListener("click", function () {
  //清除token
  localStorage.removeItem("token");
  user_username.innerHTML = "";
  user_show_img.src = "";
  user_info.style.display = "none";
  user_username.style.display = "none";
  login_btn.style.display = "block";
  logout_btn.style.display = "none";
  me_uto.innerHTML = "退出登录";
  toast_body.innerHTML = "成功退出登录！";
  toast.show();
});
//获取用户信息
var myHeaders = new Headers();
myHeaders.append("Authorization", localStorage.getItem("token"));

var urlencoded = new URLSearchParams();

var requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow",
};

fetch(`${apirUrl}/my/userinfo`, requestOptions)
  .then((response) => response.json())
  .then((result) => {
    console.log(result);
    if (result.status == 0) {
      user_username.innerHTML = result.data.username;
      user_show_img.src = apirUrl + result.data.userimg;
      user_info.style.display = "block";
      user_username.style.display = "block";
      login_btn.style.display = "none";
      logout_btn.style.display = "block";
    } else if (result.status == 1) {
      user_username.innerHTML = "";
      user_show_img.src = "";
      user_info.style.display = "none";
      user_username.style.display = "none";
      login_btn.style.display = "block";
      logout_btn.style.display = "none";
    }
  })
  .catch((error) => console.log("error", error));

function userEditText() {
  //用户操作文章
  //   获取字体变大图标
  var bi_zoom_in = document.querySelector(".bi-zoom-in");
  // 获取字体变小图标
  var bi_zoom_out = document.querySelector(".bi-zoom-out");
  // 获取行高变小图标
  var bi_arrows_collapse = document.querySelector(".bi-arrows-collapse");
  // 获取行高变大图标
  var bi_arrows_expand = document.querySelector(".bi-arrows-expand");
  //h获取字体变粗图标
  var bi_type_bold = document.querySelector(".bi-type-bold");
  //获取字体斜体图标
  var bi_type_italic = document.querySelector(".bi-type-italic");
  //获取字体下划线图标
  var bi_type_underline = document.querySelector(".bi-type-underline");
  //获取新闻内容
  var text = document.querySelector(".text");
  //点击字体变大 最大为24px 最小为12px 每次点击增加2px
  bi_zoom_in.addEventListener("click", function () {
    if (text.style.fontSize == "") {
      text.style.fontSize = "16px";
    }
    var fontSize = parseInt(text.style.fontSize);
    if (fontSize < 24) {
      text.style.fontSize = fontSize + 2 + "px";
    }
  });
  //点击字体变小
  bi_zoom_out.addEventListener("click", function () {
    if (text.style.fontSize == "") {
      text.style.fontSize = "16px";
    }
    var fontSize = parseInt(text.style.fontSize);
    if (fontSize > 12) {
      text.style.fontSize = fontSize - 2 + "px";
    }
  });
  //点击行高变小 默认40px 最小30px 最大50px
  bi_arrows_collapse.addEventListener("click", function () {
    if (text.style.lineHeight == "") {
      text.style.lineHeight = "40px";
    }
    var lineHeight = parseInt(text.style.lineHeight);
    if (lineHeight > 30) {
      text.style.lineHeight = lineHeight - 2 + "px";
    }
  });
  //点击行高变大 默认40px 最小30px 最大50px
  bi_arrows_expand.addEventListener("click", function () {
    if (text.style.lineHeight == "") {
      text.style.lineHeight = "40px";
    }
    var lineHeight = parseInt(text.style.lineHeight);
    if (lineHeight < 50) {
      text.style.lineHeight = lineHeight + 2 + "px";
    }
  });
  //点击字体交替加粗
  bi_type_bold.addEventListener("click", function () {
    if (text.style.fontWeight == "") {
      text.style.fontWeight = "normal";
    }
    if (text.style.fontWeight == "normal") {
      text.style.fontWeight = "bold";
    } else {
      text.style.fontWeight = "normal";
    }
  });
  //点击字体交替斜体
  bi_type_italic.addEventListener("click", function () {
    if (text.style.fontStyle == "") {
      text.style.fontStyle = "normal";
    }
    if (text.style.fontStyle == "normal") {
      text.style.fontStyle = "italic";
    } else {
      text.style.fontStyle = "normal";
    }
  });
  //点击字体交替下划线
  bi_type_underline.addEventListener("click", function () {
    if (text.style.textDecoration == "") {
      text.style.textDecoration = "none";
    }
    if (text.style.textDecoration == "none") {
      text.style.textDecoration = "underline";
    } else {
      text.style.textDecoration = "none";
    }
  });
}
setTimeout(userEditText, 1000);
//提示框组件
var toastTrigger = document.getElementById("liveToastBtn");
var toastLiveExample = document.getElementById("liveToast");
var toast = new bootstrap.Toast(toastLiveExample);
// var btn_login = document.querySelector(".btn-login");
var me_uto = document.querySelector(".me-auto");
var toast_body = document.querySelector(".toast-body");

function getUrlParam(newsid) {
  var url = window.location.href;
  var index = url.indexOf("?");
  var queryString = url.substring(index + 1);
  var params = queryString.split("&");
  var result = {};
  for (var i = 0; i < params.length; i++) {
    var t = params[i].split("=");
    result[t[0]] = t[1];
  }
  return result;
}
var newsid = getUrlParam("newsid");
console.log(newsid.newsid);
var requestOptions = {
  method: "GET",
  redirect: "follow",
};

// var globe_newsid;
fetch(`${apirUrl}/api/getviews/${newsid.newsid}`, requestOptions)
  .then((response) => response.json())
  .then((result) => {
    console.log(result.results);
    //设置document title 为result.title

    // globe_newsid = result.results.newsid;
    // 渲染
    if (result.code == 404) {
      location.href = "./cannotfind.html";
    }
    document.title = result.results.title;
    var data = {
      results: result.results,
      comments: result.comments,
    };
    var html = template("template", data);
    document.querySelector("#template_content").innerHTML = html;
  })

  .catch((error) => console.log("error", error));

// setTimeout(function () {
//   console.log(globe_newsid);
// }, 1200);

setTimeout(function () {
  //获取text 内容 正则获取段落两端的空格 分别替换为<p> 和 </p>标签
  var text = document.querySelector(".text");
  var text_content = text.innerHTML;
  var text_content_p = text_content.replace(/^\s+|\s+$/g, "");
  var text_content_p_p = text_content_p.replace(/\n/g, "<p>");
  var text_content_p_p_p = text_content_p_p.replace(/\s+/g, "</p><p>");
  var text_content_p_p_p_p = text_content_p_p_p.replace(/<p><\/p>/g, "");
  text.innerHTML = text_content_p_p_p_p;
}, 1100);

var recommList_ul = document.querySelector(".recommList ul");

var requestUlListOptions = {
  method: "GET",
  redirect: "follow",
};

fetch(
  `${apirUrl}/api/getrecommendnews?pageViews=1&limit=10`,
  requestUlListOptions
)
  .then((response) => response.json())
  .then((result) => {
    console.log(result.news);

    //遍历数据
    for (let i = 0; i < result.news.length; i++) {
      var recommList_lis = `
      <li class="lis"><a href="${serverUrl}/view.html?newsid=${result.news[i].newsid}">${result.news[i].title}</a></li>
        `;
      recommList_ul.innerHTML += recommList_lis;
    }
  })
  .catch((error) => console.log("error", error));
